package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired(required = false)
    private DishService dishService;
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto, HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(id);
        dishDto.setUpdateUser(id);
        dishService.save(dishDto);
        return R.success("添加成功");
    }
    @RequestMapping("/page")
    public R<Page<DishDto>> findByPage(Integer page,Integer pageSize, String name){
        Page<DishDto> pageObj = dishService.findByPage(page, pageSize, name);
        return R.success(pageObj);
    }

//    @RequestMapping("/page")
//    public R<Page<DishDto>> findByPage2(Integer page,Integer pageSize, String name){
//        Page<DishDto> pageObj = dishService.findByPage(page, pageSize, name);
//        return R.success(pageObj);
//    }

    //回显菜品数据
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    //修改
    @PutMapping
    public R<String> updateWithFlavor(@RequestBody DishDto dishDto,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(id);
        dishService.updateWithFlavors(dishDto);
        return R.success("修改成功");
    }

  /*  //根据id删除
    @DeleteMapping
    public R<String> deleteById(Long ids){
        dishService.deleteById(ids);
        return R.success("删除成功");
    }*/

    //根据id修改售卖状态
    @RequestMapping("/status/{status}")
    public R<String> updateStatus(Long[] ids,@PathVariable Integer status){
        dishService.updateStatus(ids,status);
        return R.success("修改状态成功");
    }

//    //根据id修改售卖状态
//    @RequestMapping("/status/1")
//    public R<String> updateStatus2(Long[] ids){
//        dishService.updateStatus2(ids);
//        return R.success("修改状态成功");
//    }

    //批量删除
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids){
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    //根据菜品分类id查询菜品和口味信息
    @RequestMapping("/list")
    public R<List<DishDto>> list(Long categoryId,Integer status,String name){
        List<DishDto> dishDtoList = dishService.findByCategoryIdAndStatus(categoryId,status,name);
        return R.success(dishDtoList);
    }

}
